﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonUtils;
using TERP.WaterClient.Common;
using TERP.WaterClient.DbServices;

namespace TERP.WaterClient.Views.Water
{
    public partial class FrmCustomerList : FrmMdiBase
    {
        #region Pro
		CustomerService _customerService;
        #endregion

        #region Method

        void InitControl()
        {
            OrgName.FieldName = TblCustomer.OrgName;
			CusCode.FieldName = TblCustomer.CusCode;
			cusName.FieldName = TblCustomer.CusName;
			CusAddress.FieldName = TblCustomer.Address;
			CusTel.FieldName = TblCustomer.CusTel;
			Note.FieldName = TblCustomer.Note; 

            gridView1.OptionsBehavior.Editable = false;

            this.SetGridViewIndexNormal(gridView1);

            searchData("", "", "");
        }

        void searchData(string strCusCode,string strCusName,string strOrgName)
        {
			_customerService = new CustomerService();
			DataSet dsCustomer = _customerService.Its_Customer_Get(""
																	, strCusCode
																, strCusName
																, strOrgName
																, ""
																);
			this.gridControl1.DataSource = dsCustomer.Tables[DbTable.Tbl_Customer];
        }
        #endregion

        #region Event
        public FrmCustomerList()
        {
            InitializeComponent();

            InitControl();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNhapMoi_Click(object sender, EventArgs e)
        {
            FrmCustomer frm = new FrmCustomer();
            frm.ShowDialog();
        }        

        private void btnSearch_Click(object sender, EventArgs e)
        {
            searchData(txtCusCode.Text, txtCusName.Text, txtOrgName.Text);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
			try
			{
				DataRow drow = gridView1.GetDataRow(gridView1.FocusedRowHandle);
				Water.FrmCustomer frm = new FrmCustomer(
															drow[TblCustomer.CusID].ToString()
															, drow[TblCustomer.Address].ToString()
															, drow[TblCustomer.CusCode].ToString()
															, drow[TblCustomer.CusName].ToString()
															, drow[TblCustomer.Note].ToString()
															, drow[TblCustomer.OrgName].ToString()
															, drow[TblCustomer.CusTel].ToString()
															, drow[TblCustomer.Debit].ToString()
															, drow[TblCustomer.NoVoCurrent].ToString()
															, drow[TblCustomer.CuocVoCurrent].ToString()
															);
				frm.ShowDialog();
			}
			catch (Exception ex)
			{
				Util.ShowExceptionBox(ex);
			}
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
				//try
				//{
				//    int _cusID = Convert.ToInt32(gridView1.GetFocusedRowCellValue("CusID"));

				//    _clientWaterCustomer = new ITS.Library.WaterCustomerService.WaterCustomerServiceClient();
				//    ITS.Library.WaterCustomerService.WaterCustomer _customer = _clientWaterCustomer.GetByKey(_cusID); _clientWaterCustomer.Close();
				//    Water.FrmCustomer frm = new FrmCustomer(true);
				//    frm._customer = _customer;
				//    frm._status = StatusEdit.StatusEditForm.Update;
				//    frm.ShowDialog();
				//}
				//catch (Exception ex)
				//{
				//    Util.ShowExceptionBox(ex);
				//}
            }
        }

        #endregion
    }
}
